﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebMVC.Controllers
{
    public class CongNoController : Controller
    {
        databaseDataContext db = new databaseDataContext();
        //
        // GET: /CongNo/
        [Authorize(Roles = "0")]
        public ActionResult Index()
        {
            ViewData["Items"] = from h in db.CongNos
                                select h;
            return View();
        }

        //
        // GET: /CongNo/Details/5
        [Authorize(Roles = "0")]
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /CongNo/Create
        [Authorize(Roles = "0")]
        public ActionResult Create()
        {
            IEnumerable<SelectListItem> listKhachHang = db.KhachHangs.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            IEnumerable<SelectListItem> listLoaiCongNo = db.LoaiCongNos.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            IEnumerable<SelectListItem> listNgoaiTe = db.NgoaiTes.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Don_vi_goc.ToString() + "(" + c.Ki_hieu.ToString() + ")"
            });

            ViewData["ListKhachHang"] = listKhachHang;
            ViewData["ListLoaiCongNo"] = listLoaiCongNo;
            ViewData["ListNgoaiTe"] = listNgoaiTe;
            return View();
        } 

        //
        // POST: /CongNo/Create
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Create(CongNo congno)
        {
            try
            {
                // TODO: Add insert logic here
                congno.Ngay_cap_nhat_cuoi = DateTime.Now;
                db.CongNos.InsertOnSubmit(congno);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                IEnumerable<SelectListItem> listKhachHang = db.KhachHangs.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                IEnumerable<SelectListItem> listLoaiCongNo = db.LoaiCongNos.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                IEnumerable<SelectListItem> listNgoaiTe = db.NgoaiTes.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Don_vi_goc.ToString() + "(" + c.Ki_hieu.ToString() + ")"
                });

                ViewData["ListKhachHang"] = listKhachHang;
                ViewData["ListLoaiCongNo"] = listLoaiCongNo;
                ViewData["ListNgoaiTe"] = listNgoaiTe;
                return View();
            }
        }
        
        //
        // GET: /CongNo/Edit/5
        [Authorize(Roles = "0")]
        public ActionResult Edit(int id)
        {
            CongNo model = (from m in db.CongNos
                             where m.ID == id
                             select m).SingleOrDefault();
            IEnumerable<SelectListItem> listKhachHang = db.KhachHangs.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            IEnumerable<SelectListItem> listLoaiCongNo = db.LoaiCongNos.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            IEnumerable<SelectListItem> listNgoaiTe = db.NgoaiTes.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Don_vi_goc.ToString() + "(" + c.Ki_hieu.ToString() + ")"
            });

            ViewData["ListKhachHang"] = listKhachHang;
            ViewData["ListLoaiCongNo"] = listLoaiCongNo;
            ViewData["ListNgoaiTe"] = listNgoaiTe;
            return View(model);
        }

        //
        // POST: /CongNo/Edit/5
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Edit(int id, CongNo congno)
        {
            CongNo model = (from m in db.CongNos
                            where m.ID == id
                            select m).SingleOrDefault();
            try
            {
                // TODO: Add update logic here
                
                model.Ma_khach_hang = congno.Ma_khach_hang;
                model.Ma_loai_cong_no = congno.Ma_loai_cong_no;
                model.Thanh_toan = congno.Thanh_toan;
                model.Ma_ngoai_te = congno.Ma_ngoai_te;
                model.Ty_gia = congno.Ty_gia;
                model.Thanh_tien = congno.Thanh_tien;
                model.Ghi_chu = congno.Ghi_chu;
                model.Ngay_cap_nhat_cuoi = DateTime.Now;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                IEnumerable<SelectListItem> listKhachHang = db.KhachHangs.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                IEnumerable<SelectListItem> listLoaiCongNo = db.LoaiCongNos.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                IEnumerable<SelectListItem> listNgoaiTe = db.NgoaiTes.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Don_vi_goc.ToString() + "(" + c.Ki_hieu.ToString() + ")"
                });

                ViewData["ListKhachHang"] = listKhachHang;
                ViewData["ListLoaiCongNo"] = listLoaiCongNo;
                ViewData["ListNgoaiTe"] = listNgoaiTe;
                return View(model);
            }
        }

        //
        // GET: /CongNo/Delete/5
        [Authorize(Roles = "0")]
        public ActionResult Delete(int id)
        {
            var model = (from h in db.CongNos
                         where h.ID == id
                         select h).Take(1).SingleOrDefault();
            db.CongNos.DeleteOnSubmit(model);
            db.SubmitChanges();
            return RedirectToAction("Index");
        }

        //
        // POST: /CongNo/Delete/5
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
